package com.beermanager.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.beermanager.R;
import com.beermanager.domain.Bar;
import com.beermanager.facebook.FacebookConnection;
import com.doffman.dragarea.DragArea;
import com.doffman.dragarea.DragEvent;
import com.doffman.dragarea.OnDragListener;

public class BarDraggable extends LinearLayout {
    private Context context;
    private DragArea mDragArea;

    private Bar bar;
    private ImageView mBarImage;
    private TextView mBarName;
    private com.doffman.dragarea.OnDragListener mDragListener;

    public BarDraggable(Context context, Bar bar) {
        super(context);
        this.context = context;
        initDraggableMan(bar);
    }

    public BarDraggable(Context context, AttributeSet attrs, Bar bar) {
        super(context, attrs);
        this.context = context;
        initDraggableMan(bar);
    }

    public BarDraggable(Context context, AttributeSet attrs, int defStyle, Bar bar) {
        super(context, attrs, defStyle);
        this.context = context;
        initDraggableMan(bar);
    }

    private void initDraggableMan(Bar bar){
        mBarImage = new ImageView(getContext());
        if(bar.getPicture() != null) mBarImage.setImageBitmap(bar.getPicture());
        else {
            mBarImage.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_home));
        }
        LinearLayout.LayoutParams layoutParams;
        layoutParams = new LayoutParams((int)context.getResources().getDimension(R.dimen.bar_icon_width),
                (int)context.getResources().getDimension(R.dimen.bar_icon_height));
        mBarImage.setLayoutParams(layoutParams);
        mBarImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mBarImage.setPadding(10,10,10,10);

        mBarName = new TextView(getContext());
        mBarName.setText(bar.getName());
        mBarName.setGravity(Gravity.CENTER);

        this.setOrientation(VERTICAL);
        this.setGravity(Gravity.CENTER);
        this.addView(mBarImage);
        this.addView(mBarName);
        this.bar = bar;

        initDragAreaListener();
    }

    private void initDragAreaListener(){
        mDragListener = new com.doffman.dragarea.OnDragListener()
        {
            @Override
            public void onDrag(View view, DragEvent dragEvent)
            {
                switch (dragEvent.getAction())
                {
                    case DragEvent.ACTION_DRAG_STARTED:
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        //BarDraggable.this.setVisibility(INVISIBLE);
                        setBackgroundColor(Color.LTGRAY);
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        //BarDraggable.this.setVisibility(VISIBLE);
                        setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case DragEvent.ACTION_DROP:
                        //BarDraggable.this.setVisibility(VISIBLE);
                        setBackgroundColor(Color.TRANSPARENT);
                        Toast.makeText(getContext(), bar.getName(), Toast.LENGTH_SHORT).show();
                        FacebookConnection.setBar(bar);
                        Intent intent = new Intent(context, BeerActivity.class);
                        context.startActivity(intent);
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        //BarDraggable.this.setVisibility(VISIBLE);
                        setBackgroundColor(Color.TRANSPARENT);
                        break;
                    default:
                        break;
                }
            }


        };
    }

    public void setDragArea(DragArea dragArea) {
        mDragArea = dragArea;
        dragArea.addDragListener(BarDraggable.this, mDragListener);
    }

    public void removeFromDragArea(){
        mDragArea.removeDragListener(mDragListener);
    }

    public Bar getBar() {
        return bar;
    }
}
